
package com.fx.apartment.modules.dynamic.controller;

import com.fx.apartment.modules.dynamic.service.IDynamicService;
import com.fx.apartment.common.support.Condition;
import com.fx.apartment.common.support.Query;
import com.fx.apartment.common.tool.api.R;
import com.fx.apartment.common.tool.lock.RedisLock;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.apartment.modules.dynamic.entity.Comment;
import com.fx.apartment.modules.dynamic.vo.CommentVO;
import com.fx.apartment.modules.dynamic.wrapper.CommentWrapper;
import com.fx.apartment.modules.dynamic.service.ICommentService;

import java.util.List;

/**
 * 朋友圈评论 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/comment")
@Api(value = "朋友圈评论", tags = "朋友圈评论接口")
public class CommentController {

	private final ICommentService commentService;

	private final IDynamicService dynamicService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入comment")
	public R<CommentVO> detail(Comment comment) {
		Comment detail = commentService.getOne(Condition.getQueryWrapper(comment));
		return R.data(CommentWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 朋友圈评论
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入comment")
	public R<IPage<CommentVO>> list(Comment comment, Query query) {
		IPage<Comment> pages = commentService.page(Condition.getPage(query), Condition.getQueryWrapper(comment));
		return R.data(CommentWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 朋友圈评论
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入comment")
	@RedisLock("comment::save")
	public R save(@Valid @RequestBody Comment comment) {
		return R.status(commentService.save(comment)&&dynamicService.updateCommentNum(comment.getDynamicId()));
	}

	
	/**
	 * 删除 朋友圈评论
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@RedisLock("comment::remove")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(commentService.deleteByIds(ids));
	}

	/**
	 * 未读信息
	 */
	@GetMapping("/unread-messages")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "未读信息", notes = "传入comment")
	public R unreadMessages() {
		List<CommentVO> list = commentService.unreadMessages();
		return R.data(list);
	}
	/**
	 * 读取信息
	 */
	@PostMapping("/fetch-messages")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "读取信息", notes = "传入comment")
	public R fetchMessages(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(commentService.fetchMessages(ids));
	}
}
